

class ClockComponent extends React.Component {
    constructor() {
        super()
        this.state = {
            date: new Date()
        }
    }

    componentDidMount() {
        this.timer = setInterval(() => {
            // this.setState(() => ({
            //     date: new Date()
            // }))
            this.setState(function(){
                return {
                    date: new Date()
                }
            })
        }, 1000);
    }

    componentWillUnmount() {
        clearInterval(this.timer)
    }

    render() {
        return (
            <h3>闹钟{this.state.date.toLocaleTimeString()}</h3>
        )
    }
}


let rootContainer = document.getElementById("root")
ReactDOM.render(
    <ClockComponent/>,
    rootContainer
)